package com.quasar.hpatient.widget;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.change360.calendarview.CalendarDay;
import com.quasar.hpatient.R;
import com.quasar.hpatient.onInterface.OnDateChangedEvent;
import com.quasar.hpatient.view.personal.historyRecord.LaboratoryDateActivity_;
import com.rey.material.app.DatePickerDialog;
import com.rey.material.app.DialogFragment;
import com.vise.log.ViseLog;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class WidgetDateTestRecordQuery extends LinearLayout implements View.OnClickListener {

    Context context;
    public WidgetDateTestRecordQuery(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
        this.context=context;
    }
    //根据状态跳转不同的界面

    public void setDate(CalendarDay calendarDay) {
        selectCalendar = calendarDay.getCalendar();
        date.setText(format.format(selectCalendar.getTime()));
    }

    //获取当前选定的时间
    public String getDate() {
        return date.getText().toString();
    }

    public void setDate(String TimeDate) {
        date.setText(TimeDate);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.widget_date_query, this);
        pre = (Button) findViewById(R.id.pre);
        next = (Button) findViewById(R.id.next);
        date = (TextView) findViewById(R.id.date);
        iv_search_date = (ImageView) findViewById(R.id.iv_search_date);
        pre.setOnClickListener(this);
        next.setOnClickListener(this);
        iv_search_date.setOnClickListener(this);
        selectCalendar = Calendar.getInstance();
        date.setText(format.format(selectCalendar.getTime()));
        this.context=context;
    }

    Calendar selectCalendar;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Button pre;
    Button next;
    TextView date;
    ImageView iv_search_date;

    private String Status;

    public String getStatus() {
        return Status;
    }

    public void setStatus(String status) {
        Status = status;
    }

    public WidgetDateTestRecordQuery(Context context) {
        super(context);
        init(context);
    }


    public void nextfalseImage(){
        next.setEnabled(false);
    }

    public void lastfalseImage(){
        pre.setEnabled(false);
    }

    public void nextTrueImage(){
        next.setEnabled(true);
    }

    public void lastTrueImage(){
        pre.setEnabled(true);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.pre) {
            setStatus("1");
            next.setEnabled(true);
            if (onDateChangedEvent != null) {
                onDateChangedEvent.onDateChanged(new CalendarDay(selectCalendar));
            }
        } else if (v.getId() == R.id.next) {
            setStatus("2");
//            selectCalendar.add(Calendar.DAY_OF_MONTH,1);
            Calendar now = Calendar.getInstance();
            now.set(Calendar.HOUR_OF_DAY, 23);
            if (selectCalendar.after(now)) {
                selectCalendar.add(Calendar.DAY_OF_MONTH, -1);
                next.setEnabled(false);
                return;
            } else {
                next.setEnabled(true);
            }
            if (onDateChangedEvent != null) {
                onDateChangedEvent.onDateChanged(new CalendarDay(selectCalendar));
            }
        } else if (v.getId() == R.id.iv_search_date) {
            showSelectDate();
        }

    }

    private void showSelectDate() {
        LaboratoryDateActivity_.intent(context).start();
    }

    private OnDateChangedEvent onDateChangedEvent;

    public OnDateChangedEvent getOnDateChangedEvent() {
        return onDateChangedEvent;
    }

    public void setOnDateChangedEvent(OnDateChangedEvent onDateChangedEvent) {
        this.onDateChangedEvent = onDateChangedEvent;
    }

    //隐藏日期按钮
    public void hidesearch(){
        iv_search_date.setVisibility(View.INVISIBLE);
    }
}
